package xyz.scootaloo.oj.leetcode

import org.junit.Test
import xyz.scootaloo.common.TestDsl
import xyz.scootaloo.oj.ds.ListNode
import xyz.scootaloo.oj.ds.helper.CollectionBuilder

/**
 * @author flutterdash@qq.com
 * @since 2021/7/10 15:11
 */
class P206 : TestDsl {

    private fun reverseList(head: ListNode?): ListNode? {
        var node: ListNode? = head ?: return null
        val sequence = ArrayList<ListNode>().apply {
            while (node != null) {
                add(node!!)
                node = node!!.next
            }
        }

        for (i in (sequence.size - 1) downTo 1) {
            sequence[i].next = sequence[i - 1]
        }

        sequence[0].next = null
        return sequence.last()
    }

    @Test
    fun test0() {
        CollectionBuilder.initNode(1, 2, 3, 4, 5).also {
            reverseList(it)
        }.apply {
            val node = this
            println(node)
        }
    }

}
